﻿// --------------------------------------------------------------------------------------------------------------------
// <copyright company="Microsoft Corp." file="SqlAzureServerManager.cs">
//   All rights reserved. See License.txt in the project root for license information.
// </copyright>
// 
// --------------------------------------------------------------------------------------------------------------------
namespace CloudBlitz.DeploymentManager
{
    using System.Collections.Generic;
    using System.Data.SqlClient;

    public class SqlAzureServerManager : ISqlAzureServerManager
    {
        private const string AzureSqlServerDomain = ".database.windows.net";

        private readonly SqlAzureServerCredentials credentials;

        public SqlAzureServerManager(SqlAzureServerCredentials credentials)
        {
            this.credentials = credentials;
        }

        public IList<string> ListDatabases(string serverName)
        {
            var dbServerName = string.Format("{0}{1}", serverName, AzureSqlServerDomain);

            var databases = new List<string>();

            using (
                var conn =
                    new SqlConnection(
                        string.Format(
                            "SERVER = {0}; User ID ={1}; Pwd = {2}", 
                            dbServerName, 
                            this.credentials.AdministratorLogin, 
                            this.credentials.AdministratorPassword)))
            {
                conn.Open();

                const string GetDatabaseNamesQuery = "SELECT name FROM master..sysdatabases";
                using (var databasesQuery = new SqlCommand(GetDatabaseNamesQuery, conn))
                using (var reader = databasesQuery.ExecuteReader())
                {
                    while (reader != null && reader.Read())
                    {
                        databases.Add(reader.GetString(0));
                    }
                }

                return databases;
            }
        }

        public void CreateDatabase(string serverName, string databaseName, int maxSizeInGb)
        {
            var dbServerName = string.Format("{0}{1}", serverName, AzureSqlServerDomain);

            using (
                var conn =
                    new SqlConnection(
                        string.Format(
                            "SERVER = {0}; User ID ={1}; Pwd = {2}", 
                            dbServerName, 
                            this.credentials.AdministratorLogin, 
                            this.credentials.AdministratorPassword)))
            {
                conn.Open();
                var sqlCreateDbQuery = string.Format(
                    "CREATE DATABASE {0} ( MAXSIZE = {1} GB) ", databaseName, maxSizeInGb);
                using (var createDatabaseCmd = new SqlCommand(sqlCreateDbQuery, conn))
                {
                    createDatabaseCmd.ExecuteNonQuery();
                }
            }
        }
    }
}